package com.x9space.framework.config;

import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionManager;
import org.springframework.transaction.interceptor.*;
import java.util.Collections;

/**
 * @author 忘忧症
 * @Distribute
 */
@Configuration
public class DataSourcesTransactionConfig {


    private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.x9space..*.service.*.*(..))";

    public TransactionInterceptor txAdvice(TransactionManager transactionManager) {
        //事务策略
        RuleBasedTransactionAttribute txAttr_REQUIRED = new RuleBasedTransactionAttribute();
//        DefaultTransactionAttribute txAttr_REQUIRED = new DefaultTransactionAttribute();
        txAttr_REQUIRED.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        txAttr_REQUIRED.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        txAttr_REQUIRED.setTimeout(120);

        //只读的事务策略
//        DefaultTransactionAttribute txAttr_REQUIRED_READONLY = new DefaultTransactionAttribute();
//        txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//        txAttr_REQUIRED_READONLY.setReadOnly(true);
        /*只读事务，不做更新操作*/
        RuleBasedTransactionAttribute txAttr_REQUIRED_READONLY = new RuleBasedTransactionAttribute();
        txAttr_REQUIRED_READONLY.setReadOnly(true);
        txAttr_REQUIRED_READONLY.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED );
        txAttr_REQUIRED_READONLY.setTimeout(120);

        //匹配方法名 设置对应的事务策略
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();

        source.addTransactionalMethod("add*", txAttr_REQUIRED);
        source.addTransactionalMethod("insert*", txAttr_REQUIRED);
        source.addTransactionalMethod("save*", txAttr_REQUIRED);
        source.addTransactionalMethod("delete*", txAttr_REQUIRED);
        source.addTransactionalMethod("remove*", txAttr_REQUIRED);
        source.addTransactionalMethod("update*", txAttr_REQUIRED);
        source.addTransactionalMethod("modify*", txAttr_REQUIRED);
        source.addTransactionalMethod("exec*", txAttr_REQUIRED);
        source.addTransactionalMethod("set*", txAttr_REQUIRED);
        //查询功能一般为只读
        source.addTransactionalMethod("get*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("select*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("check*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("query*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("find*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("list*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("count*", txAttr_REQUIRED_READONLY);
        source.addTransactionalMethod("is*", txAttr_REQUIRED_READONLY);
        return new TransactionInterceptor(transactionManager, source);
    }


    @Bean
    public Advisor txAdviceAdvisor(TransactionManager transactionManager) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        //配置切面
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, txAdvice(transactionManager));
    }

//    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
//        //配置切面
//        pointcut.setExpression("execution (* com.x9space..*.service.*Service.*(..))");
//
//        Class<? extends AspectJExpressionPointcut> aClass = pointcut.getClass();
//        Method getTargetShadowMatch = aClass.getDeclaredMethod("getTargetShadowMatch", Method.class, Class.class);
//        getTargetShadowMatch.setAccessible(true);
//        Class<ISysConfigService> iSysConfigServiceClass = ISysConfigService.class;
//
//        Method[] methods = iSysConfigServiceClass.getMethods();
//
//        for (Method method : methods) {
//
//
//            ShadowMatch shadowMatch= (ShadowMatch)getTargetShadowMatch.invoke(pointcut,method,ISysConfigService.class);
//            System.out.println(shadowMatch);
//        }
//    }

}
